/*
 * Copyright 2010, 2011 by the University of Richmond, Richmond, VA 23173
 * Written by Eric F. Palmer eric@ericfpalmer.com || epalmer@richmond.edu
 * And other contributors
 * http://www.richmond.edu/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project files reside at http://code.google.com/p/files-2-exist-db
 */

/*
 * demonstration of how to create a file transform
 *
 * WARNING: this example changes the contents of any file that is
 * transforms.
 */
package edu.richmond.is.webservices.files2exist.transforms;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;

import edu.richmond.is.webservices.files2exist.AbstractTransform;

public class GenericTransform extends AbstractTransform implements Serializable {

	private final static long serialVersionUID = 23947829;

	public GenericTransform(){

	}

	// transform type is currently not used

	public String toString() {
		return 
				"GenericTranform toString()" +
				"\n            transformType: " + super.getTransformType() + 
				"\n               folderName: " + super.getFolderName() +
				"\n                 hostName: " + super.getHostName() + 
				"\n                 hostPort: " + super.getHostPort() +
				"\n          existCollection: " + super.getExistCollection() +
				"\nexistPathcollectionPrefix: " + super.getExistPathCollectionPrefix() +
				"\n             SslExistSite: " + super.isSslExistSite() +
				"\n            existUsername: " + super.getExistUsername() +
				"\n            existPassword: " + super.getExistPassword() +
				"\n                 fileName: " + super.getFileName();
	}

	@Override
	public boolean transform() throws IOException {
		// you would override this method to execute the transform
		System.out.println("Performing Generic Transform");
		//TODO remove println statements
		//TODO create transform test case
		File inFile = new File(super.getFileName());
		if (inFile.isFile() && inFile.canRead() && inFile.canWrite()) {
			String fileContent = getSourceFileContent();
            // just wrap the existing xml in a transform element
			String transformedFileContent = "<transform>" + fileContent + "</transform>";
			putTargetFileContent(transformedFileContent);
		} else {
			throw new IOException("can't read the input file for the transform");
		}
		return true;
	}
	// assumes the file is encoded as a string
	// reads the file that we will transform
	private String getSourceFileContent() throws IOException {
		File f = new File(super.getFileName());
		StringBuffer fileData = new StringBuffer((int) (f.length() + 1));
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(
					new FileReader(f));
			char[] buf = new char[1024];
			int numRead=0;
			while((numRead=reader.read(buf)) != -1){
				String readData = String.valueOf(buf, 0, numRead);
				fileData.append(readData);
				buf = new char[1024];
			}
			return fileData.toString();
		} finally {
			if(reader != null) {
				reader.close();
			}
		}

	}

	// WARNING writes over source file with the new content (transformed content)
	public void putTargetFileContent(String newContent) throws IOException {
		File f = new File(this.getFileName());
		FileWriter fw = null;
		try {
			fw = new FileWriter(f);
			fw.write(newContent);
		} finally {
			if(fw != null) {
				fw.close();
			}
		}
	}
}